package com.jxb.myshop.dao.impl;


import com.jxb.myshop.dao.ProductDao;
import com.jxb.myshop.entity.Product;
import com.jxb.myshop.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: COS
 * @time: 2022/9/17 9:19
 * @description:
 */
public class ProductDaoImpl implements ProductDao {
    private QueryRunner qr = new QueryRunner(DBUtil.getDs());

    @Override
    public Product getProById(int pid) throws SQLException {
        return qr.query("select * from product where pid=?",
                new BeanHandler<>(Product.class),pid);
    }

    @Override
    public List<Product> getProByPageAndTid(int tid,int startRow, int pageSize) throws SQLException {
        return qr.query("select * from product where tid=? limit ?,?",
                new BeanListHandler<>(Product.class), tid, startRow, pageSize);
    }

    @Override
    public Long getTotalCount(int tid) throws SQLException {
        return qr.query("select count(pid) from product where tid = ?",
                new ScalarHandler<>(), tid);
    }

    @Override
    public List<Product> getAll() throws SQLException {
        return qr.query("select * from product", new BeanListHandler<>(Product.class));
    }

    @Override
    public List<Product> getAllProByTid(int tid) throws SQLException {
        return qr.query("select * from product where tid=?",
                new BeanListHandler<>(Product.class), tid);
    }
}
